package cn.yqx.bankshop.order.controller;


import cn.yqx.bankshop.order.page.PageRequest;
import cn.yqx.bankshop.order.page.PageResult;
import cn.yqx.bankshop.order.pojo.BuGoodsAddress;

import cn.yqx.bankshop.order.service.AddressService;
import cn.yqx.bankshop.result.Result;
import cn.yqx.bankshop.result.ResultCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/api/v1/address")
public class AddressController {

    private static final String PAGE_REQUEST_KEY = "pageRequest" ;
    private static final String PARAMETER_KEY = "parameter" ;
    private static final String USERID_KEY = "userId" ;

    @Autowired
    AddressService addressService ;

    @RequestMapping(value = "/find_address_by_userId",method = RequestMethod.POST)
    public Object findAddressByUserId(@RequestBody JSONObject jsonParam){
        Result result ;
        PageRequest pageRequest = JSON.parseObject(jsonParam.getJSONObject(PAGE_REQUEST_KEY).toJSONString(), PageRequest.class) ;
        JSONObject param = jsonParam.getJSONObject(PARAMETER_KEY) ;
        try{
            result = Result.success() ;
            PageResult pageResult = addressService.findAddressByUserId(pageRequest,param) ;
            result.setData(pageResult);
        }catch (Exception e) {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info(" error : {} ",e);
        }
        return result ;
    }

    @RequestMapping(value = "save_address",method = RequestMethod.POST)
    public Object saveAddress(@RequestBody  BuGoodsAddress buGoodsAddress){
        Result result ;
        try{
            log.info(JSON.toJSONString(buGoodsAddress));
            result = Result.success() ;
            int flag = addressService.saveAddress(buGoodsAddress) ;
            if(flag <= 0 ){
                result = Result.failed(ResultCode.DATA_ALREADY_EXISTS) ;
                result.setData(new JSONObject());
            }
        }catch (Exception e) {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info(" error : {} ",e);
        }
        return result ;
    }

    @RequestMapping(value = "set_isDefault_by_id" ,method = RequestMethod.GET)
    public Object setIsDefaultById(Long id,Long userId){
        Result result ;
        log.info("id is {} " , id);
        try{
            result = Result.success() ;
            int flag = addressService.setIsDefault(id,userId) ;
            if(flag <= 0 ){
                result = Result.failed(ResultCode.DATA_ALREADY_EXISTS) ;
                result.setData(new JSONObject());
            }
        }catch (Exception e) {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info(" error : {} ",e);
        }
        return result ;
    }

    @RequestMapping(value = "edit_address",method = RequestMethod.POST)
    public Object updateAddress(@RequestBody  BuGoodsAddress buGoodsAddress){
        Result result ;
        try{
            log.info(JSON.toJSONString(buGoodsAddress));
            result = Result.success() ;
            int flag = addressService.updateAddress(buGoodsAddress) ;
            if(flag <= 0 ){
                result = Result.failed(ResultCode.DATA_ALREADY_EXISTS) ;
                result.setData(new JSONObject());
            }
        }catch (Exception e) {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info(" error : {} ",e);
        }
        return result ;
    }

    @RequestMapping(value = "delete_address",method = RequestMethod.GET)
    public Object delelteAddress(Long id){
        Result result ;
        try{
            result = Result.success() ;
            int flag = addressService.deleteAddress(id) ;

        }catch (Exception e) {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info(" error : {} ",e);
        }
        return result ;
    }
}
